package leecode.array.数组其他问题;

/**
 * https://leetcode.cn/problems/minimum-size-subarray-sum/
 *
 * 长度最小的子数组
 */
public class MinSubArrayLen {
    // 采用双指针滑动窗口即可 。前缀和只能处理相等的情况
    public int minSubArrayLen(int target, int[] nums) {
        if (nums == null || nums.length <= 0) {
            return 0;
        }
        int start = 0;
        int sum = 0;
        int result = Integer.MAX_VALUE;
        for (int end = 0; end < nums.length; end++) {
            sum += nums[end];
            while (sum >= target) {
                int len = end - start + 1;
                result = Math.min(len, result);
                sum -= nums[start];
                start++;
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;
    }
}
